package com.lin.controller;

import com.lin.dataobject.*;
import com.lin.dto.UserFeatureDTO;
import com.lin.server.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017-10-17.
 */
@Controller
@RequestMapping("/common")
public class SellerCommonController {

    @Autowired
    private RegionService regionService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private ProductAttributeService productAttributeService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private FeaturesService featuresService;

    @Autowired
    private DataDictionaryService dataDictionaryService;

    /**
     * 获取下级区域
     */
    @ResponseBody
    @RequestMapping("/getRegionChildren")
    public List<TSRegion> getRegionChildren(@RequestParam(value = "pid", required = true) String pid) {
        List<TSRegion> tsRegions = regionService.getChildren(pid);
        return tsRegions;
    }

    @ResponseBody
    @RequestMapping("/getBrand")
    public List<Brand> getBrand() {
        List<Brand> brands = brandService.findAll();
        return brands;
    }

    @ResponseBody
    @RequestMapping("/getCompany")
    public List<Company> getCompany() {
        List<Company> companies = companyService.findAll();
        return companies;
    }

    @ResponseBody
    @RequestMapping("/getProductAttribute")
    public List<ProductAttribute> getProductAttribute() {
        List<ProductAttribute> productAttributes = productAttributeService.findAll();
        return productAttributes;
    }

    @ResponseBody
    @RequestMapping("/getRole")
    public List<Role> getRole() {
        List<Role> roleList = roleService.findAll(1);
        return roleList;
    }

//    @ResponseBody
//    @RequestMapping("getFeature")
//    public List<FeaturesTreeNode> getFeature(@RequestParam(value = "id", required = true) String id) {
//        List<FeaturesTreeNode> list = null;
//        try {
//            //查询到所有的组织机构信息
//            List<Features> institutions = featuresService.findAll();
//            FeatureTreeUntil treeUntil = new FeatureTreeUntil();
//            //将查询到的数据转换为树形结构的list
//            list = treeUntil.fillTree(institutions);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return list;
//    }

    @ResponseBody
    @RequestMapping("getUserFeature")
    public List<UserFeatureDTO> getUserFeature(@RequestParam(value = "id", required = true) Integer id) {
        List<Features> list=featuresService.findAll();
        Role role=roleService.findOne(id);
        String access=role.getAccess();
        String[] userAccess=new String[]{};
        if (access!=null) {
            userAccess = access.split(",");
        }
        List<UserFeatureDTO> userFeatureDTOS=new ArrayList<>();
        UserFeatureDTO dto;
        for (Features features:list){
            dto=new UserFeatureDTO();
            //如果包含子文件夹则过滤
            if (!haveChildren(list,features)) {
                if (Arrays.asList(userAccess).contains(String.valueOf(features.getId()))) {
                    dto.setChecked(true);
                }
            }
            else {
                dto.setChecked(false);
            }
            dto.setPid(features.getPid());
            dto.setId(features.getId());
            dto.setText(features.getName());
            userFeatureDTOS.add(dto);
        }
        return userFeatureDTOS;
    }

//    判断功能列表是否有子节点
    private boolean haveChildren(List<Features> list,Features features){
        for (Features f:list){
            if (Objects.equals(features.getId(), f.getPid())){
                return true;
            }
        }
        return false;
    }

    @ResponseBody
    @RequestMapping("/getDictionary")
    public List<DataDictionaryDetail> getDictionary(@RequestParam(value = "code", required = true) String code){
        return dataDictionaryService.getDetailByCode(code);
    }
}
